package com.tpsix.security;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tpsix.domain.SysUser;
import com.tpsix.security.saas.TenantContextHolder;
import com.tpsix.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("我查了数据库");
        SysUser sysUser = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));
        if (sysUser == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        return SecurityUserDetails.builder()
                .loginType(LoginType.NORMAL)
                .userId(sysUser.getId())
                .username(sysUser.getUsername())
                .password(sysUser.getPassword())
                .locked(sysUser.isLocked())
                .build();
    }
}
